package com.aternity.vbd.collect.feedback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Daniel Orr
 */
@Repository
public class FeedbackDao {

    private static final Logger logger = LoggerFactory.getLogger(FeedbackDao.class);

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void insertFeedback(FeedbackData feedbackData) {
        String sql =
                "INSERT INTO VBD_FEEDBACK " +
                        "(TIMERANGE_START ,TIMERANGE_END, APP, ACTIVITY_ID, ACTIVITY_NAME, IS_PROBLEM, COMMENTS) VALUES (?, ?, ?, ?, ?, ?, ?)";

        jdbcTemplate.update(sql,
                feedbackData.getStartTime(),
                feedbackData.getEndTime(),
                feedbackData.getApp(),
                feedbackData.getActivityId(),
                feedbackData.getActivity(),
                feedbackData.isProblem() ? "Y" : "N",
                feedbackData.getComment()
        );
    }

    public void updateFeedback(FeedbackData feedbackData) {
        assertIdNotNull(feedbackData);
        String sql =
                "update VBD_FEEDBACK " +
                        "set TIMERANGE_START=?, TIMERANGE_END=?, APP=?, ACTIVITY_ID=?, ACTIVITY_NAME=?, IS_PROBLEM=?, COMMENTS=? \n" +
                        "where id = ?";

        jdbcTemplate.update(sql,
                feedbackData.getStartTime(),
                feedbackData.getEndTime(),
                feedbackData.getApp(),
                feedbackData.getActivityId(),
                feedbackData.getActivity(),
                feedbackData.isProblem() ? "Y" : "N",
                feedbackData.getComment(),
                feedbackData.getId()
        );
    }

    public void deleteFeedback(FeedbackData feedbackData) {
        assertIdNotNull(feedbackData);
        String sql = "delete from VBD_FEEDBACK where id =?";
        jdbcTemplate.update(sql, feedbackData.getId());
    }

    private void assertIdNotNull(FeedbackData feedbackData) {
        if (feedbackData.getId() == null) {
            throw new IllegalArgumentException();
        }
    }

    public List<FeedbackData> getAllFeedbacks() {
        final String sql = "select " +
                "ID, TIMERANGE_START ,TIMERANGE_END, APP, ACTIVITY_ID, ACTIVITY_NAME, IS_PROBLEM, COMMENTS " +
                "from VBD_FEEDBACK";
        final List<FeedbackData> result = jdbcTemplate.query(
                sql, new Object[]{}, new RowMapper<FeedbackData>() {
            @Override
            public FeedbackData mapRow(ResultSet rs, int rowNum) throws SQLException {
                FeedbackData fd = new FeedbackData();
                fd.setId(rs.getInt(1));
                fd.setStartTime(rs.getDate(2));
                fd.setEndTime(rs.getDate(3));
                fd.setApp(rs.getString(4));
                fd.setActivityId(rs.getInt(5));
                fd.setActivity(rs.getString(6));
                final String isp = rs.getString(7);
                if (isp != null) {
                    fd.setProblem(isp.equals("Y"));
                } else {
                    logger.error("Bad data. missing is_problem");
                }
                fd.setComment(rs.getString(8));
                return fd;
            }
        });
        return result;
    }
}
